﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TicketManager.Data;
using TicketManager.Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web.UI.HtmlControls;

public partial class ListPhim : System.Web.UI.Page
{
    string phimType = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        phimType = Convert.ToString(Request.QueryString["phimType"]);
        BindRptPhimDangChieu(phimType);
    }

    protected void BindRptPhimDangChieu(string type)
    {
        DataTable dt = new DataTable();
        PhimFilters pFilter = new PhimFilters();

        switch (type)
        {
            case "DangChieu":
                pFilter.AppendLessThanOrEqual(PhimColumn.LichChieuBatDau, DateTime.Now.ToString());
                pFilter.AppendGreaterThanOrEqual(PhimColumn.LichChieuKetThuc, DateTime.Now.ToString());
                break;
            case "SapChieu":
                pFilter.AppendGreaterThanOrEqual(PhimColumn.LichChieuBatDau, DateTime.Now.ToString());
                pFilter.AppendGreaterThanOrEqual(PhimColumn.LichChieuKetThuc, DateTime.Now.ToString());
                break;
            default:
                break;
        }
        pFilter.Append(PhimColumn.TrangThai, "true");

        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["TicketManager.Data.ConnectionString"].ConnectionString);

        SqlDataAdapter adapter;
        string sql = "SELECT [PhimID],[TenPhim],[LichChieuBatDau],[MoTa],[TrangThai],[LichChieuKetThuc],[AnhMoTa] FROM [dbo].[phim] WHERE ";
        sql += pFilter.ToString();
        sql += " ORDER BY TenPhim ASC";
        SqlCommand commd = new SqlCommand(sql, conn);
        adapter = new SqlDataAdapter(commd);
        adapter.Fill(dt);        
        if (dt.Rows.Count > 0)
        {
            rptPhim.DataSource = dt;
            rptPhim.DataBind();
        }
    }
    protected void rptPhim_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            DataRowView drv = (DataRowView)e.Item.DataItem;
            HtmlImage imgAnh = (HtmlImage)e.Item.FindControl("imgAnh");
            HtmlAnchor aLink = (HtmlAnchor)e.Item.FindControl("aLink");
            HtmlGenericControl dvMoTa = (HtmlGenericControl)e.Item.FindControl("dvMoTa");
            imgAnh.Src = Convert.ToString(drv[PhimColumn.AnhMoTa.ToString()]);
            aLink.Title = Convert.ToString(drv[PhimColumn.TenPhim.ToString()]);
            aLink.InnerText = Convert.ToString(drv[PhimColumn.TenPhim.ToString()]);
            dvMoTa.InnerHtml = Convert.ToString(drv[PhimColumn.MoTa.ToString()]).Replace("\r\n","<br />");
        }
    }
}